<?php

namespace Admin\Models\read;

use Phalcon\Db\Column;

class Article extends BaseRead
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $title;

    /**
     *
     * @var string
     */
    public $contents;

    /**
     *
     * @var string
     */
    public $coverurl;

    /**
     *
     * @var string
     */
    public $articleurl;

    /**
     *
     * @var integer
     */
    public $articletype;        //文章类型

    /**
     *
     * @var integer
     */
    public $releasestatus;      //发布状态（1：已发布；2：未发布）

    /**
     * @var string
     */
    public $prerelease;         //预定发布时间

    /**
     *
     * @var string
     */
    public $releasetime;        //发布时间

    /**
     *
     * @var string
     */
    public $learnstages;        //所属学段(存储格式:1,2,3;说明:1：小学；2：初中；3：高中)

    /**
     *
     * @var integer
     */
    public $status;

    /**
     *
     * @var integer
     */
    public $createuserid;

    /**
     *
     * @var string
     */
    public $createtime;

    /**
     *
     * @var integer
     */
    public $updateuserid;

    /**
     *
     * @var string
     */
    public $updatetime;

    /**
     *
     * @var string
     */
    public $extra1;

    /**
     *
     * @var string
     */
    public $extra2;

    /**
     *
     * @var string
     */
    public $extra3;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setConnectionService(parent::Service);
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return parent::getTableName('article');
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Article[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Article
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 创建Model
     *
     * @param $data
     * @return Article
     *
     * create by ck 20170228
     */
    public static function createRecord($data)
    {
        $obj = !empty($data['id']) ? self::findFirst($data['id']) : new self;
        unset($data['id']);
        $obj or $obj = new self;
        $obj->assign($data);
        return $obj;
    }

    /**
     * 批量删除并返回结果
     *
     * @param $ids
     * @return bool
     *
     * create by ck 20170305
     * modify by ck 20170318
     */
    public static function deleteRecordById($ids)
    {
        if(!$ids) return true;
        return self::find([
            'conditions' => 'status = 1 AND id IN ({idArr:array})',
            'bind'       => ['idArr' => explode(',', $ids)],
            'bindType'   => ['idArr' => Column::BIND_PARAM_INT]
        ])->delete();
    }

}
